package it.slkfarm.adapter;

import java.io.File;
import java.util.List;
import java.util.Locale;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.sax.StartElementListener;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView.BufferType;
import android.widget.TextView;
import it.slkfarm.android.CropDetails;
import it.slkfarm.android.R;
import it.slkfarm.android.R.color;
import it.slkfarm.bean.Crop;
import it.slkfarm.drawable.CustomShapeDrawable;

@SuppressLint({ "NewApi", "NewApi" })
public class CropAdapter extends ArrayAdapter<Crop>
{
	private static final String imagesPath = "/data/data/it.slkfarm.android/files/";
	/**
	 * @uml.property  name="resource"
	 */
	private int resource;
	/**
	 * @uml.property  name="inflater"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="it.slkfarm.adapter.CropCacheView"
	 */
	private LayoutInflater inflater;
	/**
	 * @uml.property  name="isSummary"
	 */
	private boolean isSummary;
	/**
	 * @uml.property  name="tf"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private Typeface tf;

	
	public CropAdapter(Context context, int resourceId, List<Crop> objects, boolean isSummary, int lang) 
	{
		super(context, resourceId, objects);
		Resources res = context.getResources();
	    DisplayMetrics dm = res.getDisplayMetrics();
	    android.content.res.Configuration conf = res.getConfiguration();
		if (lang == 1)
		{
			conf.locale = Locale.getDefault();
			tf = Typeface.createFromAsset(this.getContext().getAssets(),"fonts/KMKDSP__.ttf");
		}
		else 
		{
			conf.locale = new Locale("si");
			tf = null;
		}
		res.updateConfiguration(conf, dm);
		resource = resourceId;
		inflater = LayoutInflater.from(context);	
		this.isSummary = isSummary;
	}
	
	@Override
	public View getView (int position, View convertView, ViewGroup parent) 
	{
		final Crop crop = getItem(position);
		CropCacheView cacheView;

		
		if ( convertView == null ) 
		{
			convertView = inflater.inflate(resource, null);
			cacheView = new CropCacheView(convertView);
			convertView.setTag(cacheView);
		}
		else 
		{
			convertView = convertView;
			cacheView = (CropCacheView) convertView.getTag();
		}
		RelativeLayout layout = null;
		if (resource == R.layout.crop_row)
		{
			ImageView image = cacheView.getImageViewPhoto();
			String imageName = "";
			imageName = "img"+String.valueOf(crop.getCropId())+String.valueOf(crop.getCultivarId())+".png";
					
			File f = new File(imagesPath + imageName);
			if (f.exists())
			{
				Bitmap bm = BitmapFactory.decodeFile(imagesPath + imageName);
				image.setImageBitmap(bm);
			}
			else
				image.setImageResource(R.drawable.noimage);
			
			TextView tvName = cacheView.getTextViewName();
			tvName.setTypeface(tf);
			tvName.setText(crop.getCropName());
			TextView tvCultivar = cacheView.getTextViewCultivar();
			tvCultivar.setTypeface(tf);
			tvCultivar.setText(R.string.variety);
			String app = tvCultivar.getText().toString();
			SpannableString span = new SpannableString(app + " "+crop.getCultivarName());
			span.setSpan(new ForegroundColorSpan(Color.parseColor("#43520B")), app.length()+1, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			tvCultivar.setText(span, BufferType.SPANNABLE);
			TextView tvPercentage = cacheView.getTextViewPergentage();
			tvPercentage.setTypeface(tf);
			tvPercentage.setText(R.string.percentageProduction);
			app = tvPercentage.getText().toString();
			span = new SpannableString(app + " " + String.valueOf(crop.getPercentageProduction())+"%");
			span.setSpan(new ForegroundColorSpan(Color.parseColor("#43520B")), app.length()+1, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			tvPercentage.setText(span, BufferType.SPANNABLE);
			ImageButton info = cacheView.getInfoButton();
			info.setOnClickListener(new OnClickListener() 
			{	
				public void onClick(View arg0) 
				{
					Intent i = new Intent(getContext(), CropDetails.class);
					i.putExtra(getContext().getPackageName()+".crop", crop);
					getContext().startActivity(i);
				}
			});
			
			layout = cacheView.getRelativeLayout();
			RoundRectShape rs = new RoundRectShape(new float[] { 7, 7, 7, 7, 7, 7, 7, 7 }, null, null);
		    ShapeDrawable sd = new CustomShapeDrawable(rs, crop.getPercentageProduction(), 5, crop.isSelected());
		    layout.setBackgroundDrawable(sd);
		}
		else
		{
			ImageView image = cacheView.getImageViewPhotoSummary();
			String imageName = "";
			imageName = "img"+String.valueOf(crop.getCropId())+String.valueOf(crop.getCultivarId())+".png";
					
			File f = new File(imagesPath + imageName);
			if (f.exists())
			{
				Bitmap bm = BitmapFactory.decodeFile(imagesPath + imageName);
				image.setImageBitmap(bm);
			}
			else
				image.setImageResource(R.drawable.noimage);
			TextView tvName = cacheView.getTextViewNameSummary();
			tvName.setTypeface(tf);
			tvName.setText(crop.getCropName());
			TextView tvQuantity = (TextView) cacheView.getTextViewQuantity();
			tvQuantity.setTypeface(tf);
			tvQuantity.setText(R.string.quantity);
			String app = tvQuantity.getText().toString();
			SpannableString span = new SpannableString(app + " "+crop.getQuantity()+"q");
			span.setSpan(new ForegroundColorSpan(Color.parseColor("#43520B")), app.length()+1, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			tvQuantity.setText(span, BufferType.SPANNABLE);
			
			TextView tvCultivar = cacheView.getTextViewCultivarSummary();
			tvCultivar.setTypeface(tf);
			tvCultivar.setText(R.string.variety);
			app = tvCultivar.getText().toString();
			span = new SpannableString(app + " "+crop.getCultivarName());
			span.setSpan(new ForegroundColorSpan(Color.parseColor("#43520B")), app.length()+1, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			tvCultivar.setText(span, BufferType.SPANNABLE);
			ImageButton delete = cacheView.getDeleteButton();
			if (crop.getProductionId() != -1)
				delete.setVisibility(View.GONE);
			else
				delete.setVisibility(View.VISIBLE);
			delete.setOnClickListener(new OnClickListener() 
			{	
				public void onClick(View v) 
				{
					// TODO Auto-generated method stub
					Log.i("click", "button delete");
					remove(crop);
					notifyDataSetChanged();
				}
			});
			
			layout = cacheView.getRelativeLayoutSummary();
			RoundRectShape rs = new RoundRectShape(new float[] { 7, 7, 7, 7, 7, 7, 7, 7 }, null, null);
		    ShapeDrawable sd = new CustomShapeDrawable(rs, crop.getPercentageProduction(), 5, isSummary);
		    layout.setBackgroundDrawable(sd);
		}
		return convertView;
	}

}
